package com.ygqh.baby.observer;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.constant.Constant;
import com.ygqh.baby.service.YgVoteHelpService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Observable;
import java.util.Observer;

/**
 * 投票
 *
 * @author: sunshuo
 * @date: 2018/12/25 16:08
 */
@Component
public class VoteHelpObserver implements Observer {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private YgVoteHelpService ygVoteHelpService;

	@Override
	public void update(Observable o, Object arg) {
		ChannelScanModel model = arg != null ? (ChannelScanModel) arg : null;
		if (model == null) {
			logger.error("【VoteHelpObserver】参数异常");
			return;
		}
		if (StringUtils.isBlank(model.getSeoId()) || !model.getSeoId().startsWith(Constant.VOTE_WX_HELP_SOURCE_PREFIX)) {
			logger.info("【VoteHelpObserver】非投票sourceCode，seoid={}", model.getSeoId());
			return;
		}
		Message message = ygVoteHelpService.addWxHelp(model.getSeoId(), model.getOpenId());
		if (Message.isSuccess(message)) {
			model.setContent(message.getContent());
		} else {
			logger.warn("【VoteHelpObserver】" + message.getContent());
		}
	}

}
